﻿// Copyright (c) 2012 - 2020 Jared Zheng <jaredz at outlook dot com>
// Buddha Framework is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
//          http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
//
// Create : 2012-12-01 version 0.1 Jared Zheng <jaredz at outlook dot com>
// Update : 2018-12-05 version 3.1.5 Jared Zheng <jaredz at outlook dot com>

#include "stdafx.h"
#include "resource.h"
#include "ServerLoader.h"
#include "CenterDlg.h"

///////////////////////////////////////////////////////////////////////////////////////////////////////////
// CCenterDlg
XBEGIN_DLG_ID_WND_EXCHANGE( CCenterDlg )
XDLG_ID_WND( IDC_LISTEN_INTERNAL, m_ListenInternal )
XDLG_ID_WND( IDC_CENTER_INFO,     m_ServerInfo  )
XDLG_ID_WND( IDD_BALANCE,         m_BalanceSvr  )
XDLG_ID_WND( IDD_AUTHOR,          m_AuthorSvr   )
XDLG_ID_WND( IDD_MASTER,          m_MasterSvr   )
XDLG_ID_WND( IDD_AUTHORDB,        m_AuthorDBSvr )
XDLG_ID_WND( IDD_MASTERDB,        m_MasterDBSvr )
XEND_DLG_ID_WND_EXCHANGE( CXDlg )

CCenterDlg::CCenterDlg(void)
: m_uCenterOnline(0)
, m_uCenterTotal(0)
, m_uCenterLoad(0)
, m_uBalanceCount(0)
, m_uBalanceAddr(0)
, m_uBalanceNotice(0)
, m_uAuthorDBCount(0)
, m_uAuthorDBServer(0)
, m_uAuthorDBQuery(0)
, m_uAuthorCount(0)
, m_uAuthorOnline(0)
, m_uAuthorTotal(0)
, m_uMasterDBCount(0)
, m_uMasterDBServer(0)
, m_uMasterDBQuery(0)
, m_uMasterCount(0)
, m_uMasterGate(0)
, m_uMasterService(0)
{
}

CCenterDlg::~CCenterDlg(void)
{
}

LRESULT CCenterDlg::OnInitDialog(void)
{
    RECT rc;
    GetClientRect(&rc);
    rc.top += (DLG_ITEM_GRAP + DLG_ITEM_GRAP + DLG_ITEM_GRAP + DLG_ITEM_HEIGHT);
    rc.top += (DLG_ITEM_GRAP + DLG_ITEM_GRAP + DLG_ITEM_GRAP + DLG_ITEM_HEIGHT);

    Long lHeight = (rc.bottom - rc.top);
    if (lHeight > 640)
    {
        lHeight /= 5;
        m_BalanceSvr.MoveWindow(0, rc.top, rc.right, lHeight);
        rc.top += (DLG_ITEM_GRAP + lHeight);
        m_AuthorSvr.MoveWindow(0, rc.top, rc.right, lHeight);
        rc.top += (DLG_ITEM_GRAP + lHeight);
        m_MasterSvr.MoveWindow(0, rc.top, rc.right, lHeight);
        rc.top += (DLG_ITEM_GRAP + lHeight);
        m_AuthorDBSvr.MoveWindow(0, rc.top, rc.right, lHeight);
        rc.top += (DLG_ITEM_GRAP + lHeight);
        m_MasterDBSvr.MoveWindow(0, rc.top, rc.right, lHeight);

        m_AuthorDBSvr.ShowWindow(SW_SHOW);
        m_MasterDBSvr.ShowWindow(SW_SHOW);
    }
    else if (lHeight > 512)
    {
        lHeight /= 4;
        m_BalanceSvr.MoveWindow(0, rc.top, rc.right, lHeight);
        rc.top += (DLG_ITEM_GRAP + lHeight);
        m_AuthorSvr.MoveWindow(0, rc.top, rc.right, lHeight);
        rc.top += (DLG_ITEM_GRAP + lHeight);
        m_MasterSvr.MoveWindow(0, rc.top, rc.right, lHeight);
        rc.top += (DLG_ITEM_GRAP + lHeight);
        m_AuthorDBSvr.MoveWindow(0, rc.top, rc.right, lHeight);

        m_AuthorDBSvr.ShowWindow(SW_SHOW);
        m_MasterDBSvr.ShowWindow(SW_HIDE);
    }
    else
    {
        lHeight /= 3;
        m_BalanceSvr.MoveWindow(0, rc.top, rc.right, lHeight);
        rc.top += (DLG_ITEM_GRAP + lHeight);
        m_AuthorSvr.MoveWindow(0, rc.top, rc.right, lHeight);
        rc.top += (DLG_ITEM_GRAP + lHeight);
        m_MasterSvr.MoveWindow(0, rc.top, rc.right, lHeight);

        m_AuthorDBSvr.ShowWindow(SW_HIDE);
        m_MasterDBSvr.ShowWindow(SW_HIDE);
    }
    //
    CStringFix strTemp;
    strTemp.Load(IDC_LISTEN_INTERNAL);
    m_ListenInternal.SetWindowText(*strTemp);
    //
    InitServerData(PAKT_BALANCE);
    InitServerData(PAKT_AUTHOR);
    InitServerData(PAKT_MASTER);
    InitServerData(PAKT_AUTHORDB);
    InitServerData(PAKT_MASTERDB);
    return TRUE;
}

LRESULT CCenterDlg::OnSize(WPARAM, LPARAM lParam, BOOL&)
{
    RECT rc   = { 0 };
    rc.right  = (Long)LOWORD(lParam);
    rc.bottom = (Long)HIWORD(lParam);

    rc.top += (DLG_ITEM_GRAP + DLG_ITEM_GRAP + DLG_ITEM_GRAP + DLG_ITEM_HEIGHT);
    m_ServerInfo.MoveWindow(0, rc.top, rc.right, DLG_ITEM_HEIGHT);

    rc.top += (DLG_ITEM_GRAP + DLG_ITEM_GRAP + DLG_ITEM_GRAP + DLG_ITEM_HEIGHT);

    Long lHeight = (rc.bottom - rc.top);
    if (lHeight > 640)
    {
        lHeight /= 5;
        m_BalanceSvr.MoveWindow(0, rc.top, rc.right, lHeight);
        rc.top += (DLG_ITEM_GRAP + lHeight);
        m_AuthorSvr.MoveWindow(0, rc.top, rc.right, lHeight);
        rc.top += (DLG_ITEM_GRAP + lHeight);
        m_MasterSvr.MoveWindow(0, rc.top, rc.right, lHeight);
        rc.top += (DLG_ITEM_GRAP + lHeight);
        m_AuthorDBSvr.MoveWindow(0, rc.top, rc.right, lHeight);
        rc.top += (DLG_ITEM_GRAP + lHeight);
        m_MasterDBSvr.MoveWindow(0, rc.top, rc.right, lHeight);

        m_AuthorDBSvr.ShowWindow(SW_SHOW);
        m_MasterDBSvr.ShowWindow(SW_SHOW);
    }
    else if (lHeight > 512)
    {
        lHeight /= 4;
        m_BalanceSvr.MoveWindow(0, rc.top, rc.right, lHeight);
        rc.top += (DLG_ITEM_GRAP + lHeight);
        m_AuthorSvr.MoveWindow(0, rc.top, rc.right, lHeight);
        rc.top += (DLG_ITEM_GRAP + lHeight);
        m_MasterSvr.MoveWindow(0, rc.top, rc.right, lHeight);
        rc.top += (DLG_ITEM_GRAP + lHeight);
        m_AuthorDBSvr.MoveWindow(0, rc.top, rc.right, lHeight);

        m_AuthorDBSvr.ShowWindow(SW_SHOW);
        m_MasterDBSvr.ShowWindow(SW_HIDE);
    }
    else
    {
        lHeight /= 3;
        m_BalanceSvr.MoveWindow(0, rc.top, rc.right, lHeight);
        rc.top += (DLG_ITEM_GRAP + lHeight);
        m_AuthorSvr.MoveWindow(0, rc.top, rc.right, lHeight);
        rc.top += (DLG_ITEM_GRAP + lHeight);
        m_MasterSvr.MoveWindow(0, rc.top, rc.right, lHeight);

        m_AuthorDBSvr.ShowWindow(SW_HIDE);
        m_MasterDBSvr.ShowWindow(SW_HIDE);
    }

    return 0;
}

void CCenterDlg::OnLive(bool bStart)
{
    if (bStart)
    {
        CStrAddr strAddr;

        CStringFix strTemp;
        strTemp.Load(IDC_LISTEN_INTERNAL);
        GServerLoaderInst->m_Config.GetServerAddr(CServerConfig::CFG_LOAD_CENTER, CServerConfig::CFG_LOAD_NONE, strAddr);
        strTemp.AppendFormat(TF("[%s]:%d"), *strAddr.strIp, strAddr.usPort);
        m_ListenInternal.SetWindowText(*strTemp);
    }
    else
    {
        CStringFix strTemp;
        strTemp.Load(IDC_LISTEN_INTERNAL);
        m_ListenInternal.SetWindowText(*strTemp);

        m_uCenterOnline   = 0;
        m_uCenterTotal    = 0;
        m_uCenterLoad     = 0;
        m_uBalanceCount   = 0;
        m_uBalanceAddr    = 0;
        m_uBalanceNotice  = 0;
        m_uAuthorDBCount  = 0;
        m_uAuthorDBServer = 0;
        m_uAuthorDBQuery  = 0;
        m_uAuthorCount    = 0;
        m_uAuthorOnline   = 0;
        m_uAuthorTotal    = 0;
        m_uMasterDBCount  = 0;
        m_uMasterDBServer = 0;
        m_uMasterDBQuery  = 0;
        m_uMasterCount    = 0;
        m_uMasterGate     = 0;
        m_uMasterService  = 0;
    }
    m_ServerInfo.SetWindowText(TF(""));
}

void CCenterDlg::OnLink(Int nServerIndex, uintptr_t utData)
{
    AddServerData(nServerIndex, utData);
}

void CCenterDlg::OnUpdate(Int nServerIndex, uintptr_t utData)
{
    UpdateServerData(nServerIndex, utData);
}

void CCenterDlg::OnSync(Int nServerIndex, uintptr_t utData)
{
    PSERVER_STATUS pStatus = reinterpret_cast<PSERVER_STATUS>(utData);
    switch (nServerIndex)
    {
    case PAKT_MASTER:
        {
            m_uMasterGate    = pStatus->uTotal;
            m_uMasterService = pStatus->uOnline;
            m_uMasterCount   = pStatus->usLoad;
        }
        break;
    case PAKT_MASTERDB:
        {
            m_uMasterDBServer = pStatus->uTotal;
            m_uMasterDBQuery  = pStatus->uOnline;
            m_uMasterDBCount  = pStatus->usLoad;
        }
        break;
    case PAKT_AUTHOR:
        {
            m_uAuthorTotal  = pStatus->uTotal;
            m_uAuthorOnline = pStatus->uOnline;
            m_uAuthorCount  = pStatus->usLoad;
        }
        break;
    case PAKT_AUTHORDB:
        {
            m_uAuthorDBServer = pStatus->uTotal;
            m_uAuthorDBQuery  = pStatus->uOnline;
            m_uAuthorDBCount  = pStatus->usLoad;
        }
        break;
    case PAKT_BALANCE:
        {
            m_uBalanceAddr   = pStatus->uTotal;
            m_uBalanceNotice = pStatus->uOnline;
            m_uBalanceCount  = pStatus->usLoad;
        }
        break;
    default:
        {
            assert(nServerIndex == PAKT_CENTER);
            m_uCenterTotal  = pStatus->uTotal;
            m_uCenterOnline = pStatus->uOnline;
            m_uCenterLoad   = pStatus->usLoad / DATAD_TOPERCENT;
        }
    }
}

void CCenterDlg::OnSync(Int, CStream&)
{
}

void CCenterDlg::OnUnlink(Int nServerIndex, uintptr_t utData)
{
    RemoveServerData(nServerIndex, utData);
}

void CCenterDlg::UpdateServerInfo(void)
{
    CStringFix strInfo;
    strInfo.Load(IDC_CENTER_INFO);

    CStringFix strTemp;
    strTemp.Format(*strInfo, m_uCenterOnline, m_uCenterTotal, m_uCenterLoad,
                   m_uBalanceCount,  m_uBalanceAddr,    m_uBalanceNotice,
                   m_uAuthorCount,   m_uAuthorOnline,   m_uAuthorTotal,
                   m_uMasterCount,   m_uMasterGate,     m_uMasterService,
                   m_uAuthorDBCount, m_uAuthorDBServer, m_uAuthorDBQuery,
                   m_uMasterDBCount, m_uMasterDBServer, m_uMasterDBQuery);
    m_ServerInfo.SetWindowText(*strTemp);
}

CXListView* CCenterDlg::GetListView(Int nServerIndex)
{
    switch (nServerIndex)
    {
    case PAKT_MASTER:
        {
            return &m_MasterSvr;
        }
        break;
    case PAKT_AUTHOR:
        {
            return &m_AuthorSvr;
        }
        break;
    case PAKT_BALANCE:
        {
            return &m_BalanceSvr;
        }
        break;
    case PAKT_AUTHORDB:
        {
            return &m_AuthorDBSvr;
        }
        break;
    case PAKT_MASTERDB:
        {
            return &m_MasterDBSvr;
        }
        break;
    default:
        {
        }
    }
    return nullptr;
}

